package com.boot_reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot_reggie.Service.impl.CateServiceimpl;
import com.boot_reggie.Service.impl.Dishserviceimpl;
import com.boot_reggie.Service.impl.SetmealServiceimpl;
import com.boot_reggie.common.CustomException;
import com.boot_reggie.common.Result;
import com.boot_reggie.domain.Category;
import com.boot_reggie.domain.Dish;
import com.boot_reggie.domain.Setmeal;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/class")
public class CategoryController {
    @Autowired
    private SetmealServiceimpl setmealServiceimpl;
    @Autowired
    private Dishserviceimpl dishserviceimpl;
    @Autowired
    private CateServiceimpl serviceimpl;
    @PostMapping("food")
    public Result<String> save(HttpServletRequest res, @RequestBody Category category){
        boolean data = serviceimpl.save(category);
        if(data){
            return Result.success("数据添加成功");
        }
        return Result.error("接口异常");

    };
    @GetMapping("getAll")
    public Result<IPage> getAll(HttpServletRequest req, int page, int size){

        IPage page1 = new Page(page,size);
        page1 = serviceimpl.getPage(page1);
        if(page > page1.getPages()){
            page1 = new Page(page1.getPages(),size);
            page1 = serviceimpl.getPage(page1);
            return Result.pageData(page1);
        };
        System.out.println(page1.getRecords());
        return Result.pageData(page1);
    }
    @PutMapping("food")
    public Result<String> updata(@RequestBody Category category){
        try{
            boolean data = serviceimpl.updateById(category);
            if(!data){
                return Result.error("数据提交有误");
            };
        }catch (Exception ex){
            return Result.error("后端接口异常");
        }

        return Result.success("数据更新成功");
    };
    @DeleteMapping("food")
    public Result<String> delete(@RequestBody Category category){
        /**/
        //        先查询分类底下是否有套餐绑定
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
//        如果是套餐则判断是否有套餐表存在
        queryWrapper.eq(Setmeal::getCategoryId,category.getId());
        int num = setmealServiceimpl.count(queryWrapper);
        LambdaQueryWrapper<Dish> lambdaQueryWrappers = new LambdaQueryWrapper();
//        如果是菜品分类则检查是否有菜品绑定
        lambdaQueryWrappers.eq(Dish::getCategoryId,category.getId());
        int count = dishserviceimpl.count(lambdaQueryWrappers);
        if(num > 0 || count > 0){

            throw new CustomException("分类下有套餐或菜品绑定不能删除");

        }
        /**/
        try{
            boolean data = serviceimpl.removeById(category);
//            boolean data = serviceimpl.delData(category);
            if(!data){
                return Result.error("分类删除失败");
            }
        }catch (Exception ex){
            System.out.println(ex);
            return Result.error("后端接口异常");
        }
        return Result.success("删除成功");
    }
//    查询分类列表
    @GetMapping("list")
    public Result<List<Category>> list(Category category){
        System.out.println("======="+category.getType());
        LambdaQueryWrapper<Category> lb =  new LambdaQueryWrapper<>();
        lb.eq(category.getType() != null,Category::getType,category.getType());
        lb.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> arr = serviceimpl.list(lb);
        System.out.println(arr);
        return Result.success(arr,"数据查询成功");
    }
    @GetMapping("class")
    public Result<List<Category>> lists(){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Category::getType,2);
        List<Category> arr = serviceimpl.list(queryWrapper);
        return Result.success(arr,"数据查询成功");
    }
}

